﻿using System;
using System.Configuration;
using System.Linq;
using System.Windows.Forms;
using mki.Backup.GUI.Enumerations;
using mki.Backup.GUI.Properties;
using System.Threading;
using System.Collections;
using mki.Backup.Objects;

namespace mki.Backup.GUI.Helper
{
	/// <summary>
	/// Represents the TreeViewExtension class
	/// </summary>
	internal static class TreeViewExtension
	{
		#region Methods (4)

		// Public Static Methods (3) 

		/// <summary>
		/// Creates the context menu.
		/// </summary>
		/// <param name="source">The source.</param>
		/// <param name="contextMenu">The context menu.</param>
		public static void CreateContextMenu(this TreeView source, ContextMenuStrip contextMenu)
		{
			contextMenu.Items.Clear();
			contextMenu.Items.Add(createContextMenuItem(Resources.ContextMenuItemAddBackupJob, ButtonAction.AddJob));
		}

		/// <summary>
		/// Creates the context menu.
		/// </summary>
		/// <param name="source">The <see cref="System.Windows.Forms.TreeNodeMouseClickEventArgs"/> instance containing the event data.</param>
		/// <param name="contextMenu">The context menu.</param>
		public static void CreateContextMenu(this TreeNodeMouseClickEventArgs source, ContextMenuStrip contextMenu)
		{
			if (source.Button != MouseButtons.Right) { return; }

			contextMenu.Items.Clear();
			var createRemoveItem = false;

			// create context menu with node specific options
			if (source.Node.Tag is ConfigurationElementCollection)
			{
				var backupSection = source.Node.Tag as DBBackupSection;
				if (backupSection != null) { contextMenu.Items.Add(createContextMenuItem(Resources.ContextMenuItemAddDatabaseBackup, ButtonAction.AddDatabaseBackup)); }
				var runProcessSection = source.Node.Tag as RunProcessSection;
				if (runProcessSection != null) { contextMenu.Items.Add(createContextMenuItem(Resources.ContextMenuItemAddProcess, ButtonAction.AddProcess)); }
				var ftpSection = source.Node.Tag as FTPJobSection;
				if (ftpSection != null) { contextMenu.Items.Add(createContextMenuItem(Resources.ContextMenuItemAddFTP, ButtonAction.AddFTP)); }
				var zipSection = source.Node.Tag as ZipJobSection;
				if (zipSection != null) { contextMenu.Items.Add(createContextMenuItem(Resources.ContextMenuItemAddZIP, ButtonAction.AddZIP)); }

				createRemoveItem = false;
			}

			// backup job context menu items
			var backupJob = source.Node.Tag as BackupJobElement;
			if (backupJob != null)
			{
				if (source.Node.Nodes.Cast<TreeNode>().Where(t => t.Text == Resources.NodeDBBackupJobs).Count() <= 0) { contextMenu.Items.Add(createContextMenuItem(Resources.ContextMenuItemAddBackupJob, ButtonAction.AddDBBackupJob)); }
				if (source.Node.Nodes.Cast<TreeNode>().Where(t => t.Text == Resources.NodeRunProcessJobs).Count() <= 0) { contextMenu.Items.Add(createContextMenuItem(Resources.ContextMenuItemAddRunProcessJob, ButtonAction.AddRunProcessJob)); }
				if (source.Node.Nodes.Cast<TreeNode>().Where(t => t.Text == Resources.NodeZipJobs).Count() <= 0) { contextMenu.Items.Add(createContextMenuItem(Resources.ContextMenuItemAddZIPJob, ButtonAction.AddZIPJob)); }
				if (source.Node.Nodes.Cast<TreeNode>().Where(t => t.Text == Resources.NodeFTPJobs).Count() <= 0) { contextMenu.Items.Add(createContextMenuItem(Resources.ContextMenuItemAddFTPJob, ButtonAction.AddFTPJob)); }

				if (source.Node.GetNodeCount(true) <= 0) { createRemoveItem = true; }
			}

			// db backup menu items
			if (source.Node.Tag is DBBackupElement) { createRemoveItem = true; }

			// run process menu items
			if (source.Node.Tag is RunProcessElement) { createRemoveItem = true; }

			// ftp context menu items
			var ftpJob = source.Node.Tag as FTPJobElement;
			if (ftpJob != null)
			{
				contextMenu.Items.Add(createContextMenuItem(Resources.ContextMenuItemAddFileUpload, ButtonAction.AddFTPFileUpload));
				contextMenu.Items.Add(createContextMenuItem(Resources.ContextMenuItemAddDeleteAction, ButtonAction.AddFTPDeleteAction));
				createRemoveItem = true;
			}

			if (source.Node.Tag is FTPAddElement || source.Node.Tag is FTPDeleteElement) { createRemoveItem = true; }

			// zip context menu items
			var zipJob = source.Node.Tag as ZipJobElement;
			if (zipJob != null)
			{
				contextMenu.Items.Add(createContextMenuItem(Resources.ContextMenuItemAddFile, ButtonAction.AddZipFile));
				contextMenu.Items.Add(createContextMenuItem(Resources.ContextMenuItemAddFolder, ButtonAction.AddZipFolder));
				createRemoveItem = true;
			}

			if (source.Node.Tag is ZipAddFileElement || source.Node.Tag is ZipAddFolderElement) { createRemoveItem = true; }

			// need to create remove item?
			if (createRemoveItem) { contextMenu.Items.Add(createContextMenuItem(Resources.ContextMenuItemRemove, ButtonAction.Remove)); }
		}

		/// <summary>
		/// Loads the images.
		/// </summary>
		/// <param name="source">The source.</param>
		public static void LoadImages(this TreeView source)
		{
			source.ImageList = new ImageList();

			var query = from r in Resources.ResourceManager.GetResourceSet(Thread.CurrentThread.CurrentCulture, true, true).Cast<DictionaryEntry>().ToList()
						where Equals(r.Value.GetType(), typeof(System.Drawing.Bitmap))
						select r;

			foreach (var image in query)
			{
				source.ImageList.Images.Add(image.Key.ToString(), image.Value as System.Drawing.Image);
			}
		}
		// Private Static Methods (1) 

		/// <summary>
		/// Creates the context menu item.
		/// </summary>
		/// <param name="text">The text.</param>
		/// <param name="tag">The tag.</param>
		/// <param name="imageKey">The image key.</param>
		/// <returns>
		/// returns a object of type ToolStripMenuItem
		/// </returns>
		private static ToolStripMenuItem createContextMenuItem(String text, Object tag, String imageKey = null)
		{
			return new ToolStripMenuItem(text) { Tag = tag, ImageKey = imageKey };
		}

		#endregion Methods
	}
}